﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class AutocompleteHelpers
    {
        public static MvcHtmlString AutocompleteFor<T, TReturn>(
            this HtmlHelper<T> html,
            Expression<Func<T, TReturn>> prop,
            object value = null,
            string propId = null,
            object propIdValue = null,
            string controller = null,
            int? maxResults = null,
            string parentId = null,
            int? minLength = null,
            int? delay = null,
            object htmlAttributes = null,
            bool generatePropId = true,
            string prefix = null,
            IDictionary<string, string> data = null,
            string area = null)
        {
            return Autocomplete(
                html, 
                ExpressionHelper.GetExpressionText(prop), 
                value, 
                propId, 
                propIdValue, 
                controller, 
                maxResults, 
                parentId, 
                minLength, 
                delay, 
                htmlAttributes,
                generatePropId,
                prefix,
                data,
                area);
        }

        public static MvcHtmlString Autocomplete(this HtmlHelper html, AutocompleteParams o)
        {
            o.PropId = o.PropId ?? o.Prop.MakePropId(Settings.Autocomplete.IdFormat);

            o.Value = o.Value.FromModel(html, o.Prop);
            o.PropIdValue = o.PropIdValue.FromModel(html, o.PropId);
            
            o.ReadMeta(html);

            o.Controller = o.Controller ?? o.Prop.GetController() + "Autocomplete";

            var hd = o.HtmlAttributes.GetDictionary();
            var h = !html.ViewData.ModelState.IsValidField(o.Prop)
                        ? hd.AddVal("class", "input-validation-error")
                        : hd;

            o.ReadSettings(typeof(Settings.Autocomplete));

            var info = new AutocompleteInfo();
            info.ReadParams(o);
            info.HtmlAttributes = h.ToHtml();

            return html.Partial(Settings.AwesomeFolder + "Autocomplete.ascx", info);
        }

        public static MvcHtmlString Autocomplete(
            this HtmlHelper html,
            string prop,
            object value = null,
            string propId = null,
            object propIdValue = null,
            string controller = null,
            int? maxResults = null,
            string parentId = null,
            int? minLength = null,
            int? delay = null,
            object htmlAttributes = null,
            bool generatePropId = true,
            string prefix = null,
            IDictionary<string, string> data = null,
            string area = null)
        {
            return
                html.Autocomplete(new AutocompleteParams
                                      {
                                          Controller = controller,
                                          Data = data,
                                          Delay = delay,
                                          GeneratePropId = generatePropId,
                                          HtmlAttributes = htmlAttributes,
                                          MaxResults = maxResults,
                                          MinLength = minLength,
                                          ParentId = parentId,
                                          Prefix = prefix,
                                          Prop = prop,
                                          PropId = propId,
                                          PropIdValue = propIdValue,
                                          Value = value,
                                          Area = area
                                      });
        }
    }
}